//
//  DMMessageCellModel.m
//  BaseAPP
//
//  Created by Evisu on 2017/8/7.
//  Copyright © 2017年 evisum. All rights reserved.
//


#import "DMMessageCellModel.h"

#define margin 10
#define marginTop 7
#define headImageWH 40

@implementation DMMessageCellModel

- (void)setMessage:(DMMessageModel *)message {

    _message = message;
    
    if (message.direction == MessageDirectionReceive) {
        
        /******************接收消息*****************/
        //头像frame
        _headImageFrame = CGRectMake(margin, marginTop+2, headImageWH, headImageWH);
        
        switch (message.type) {
            case MessageTypeText:
            {
                //文字消息
                //计算文字的高度
                CGFloat maxLabelWidth = KSCREEN_WIDTH * 0.58;
                CGRect rect = [message.text boundingRectWithSize:CGSizeMake(maxLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]} context:nil];
                
                //气泡frame
                CGFloat bubbleWidth = rect.size.width+10+16<52?50:rect.size.width+10+20;
                _bubbleFrame = CGRectMake(margin+headImageWH+4, marginTop, bubbleWidth, rect.size.height+28);
                
                //文字frame
                _labelFrame = CGRectMake(16, 14, rect.size.width, rect.size.height);
                _maxCellHeight = rect.size.height+28+2*marginTop;
                
            }
                break;
                
            case MessageTypeImage:{
                //图片消息
                //气泡frame
                _bubbleFrame = CGRectMake(0, 0, message.thumbnailSize.width, message.thumbnailSize.height);
                _photoViewFrame = CGRectMake(margin+headImageWH+4, 4, _bubbleFrame.size.width, _bubbleFrame.size.height);
                _maxCellHeight = message.thumbnailSize.height + 2*marginTop;
                
            }
                break;
                
            default:
                break;
        }
        
        
    } else if (message.direction == MessageDirectionSend) {
        
        /******************发送消息*****************/
        //头像frame
        _headImageFrame = CGRectMake(KSCREEN_WIDTH-margin-headImageWH, marginTop+2, headImageWH, headImageWH);
        
        switch (message.type) {
            case MessageTypeText:
            {
                //文字消息
                //计算文字的高度
                CGFloat maxLabelWidth = KSCREEN_WIDTH * 0.58;
                CGRect rect = [message.text boundingRectWithSize:CGSizeMake(maxLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]} context:nil];
                
                //气泡frame
                CGFloat bubbleWidth = rect.size.width+10+16<52?50:rect.size.width+10+18;
                _bubbleFrame = CGRectMake(KSCREEN_WIDTH-margin-headImageWH-4-bubbleWidth, marginTop, bubbleWidth, rect.size.height+28);
                
                //文字frame
                _labelFrame = CGRectMake(14, 14, rect.size.width, rect.size.height);
                _maxCellHeight = rect.size.height+28+2*marginTop;
                
                
            }
                break;
                
            case MessageTypeImage:{
                //图片消息
                //气泡frame
                _bubbleFrame = CGRectMake(0, 0, message.thumbnailSize.width, message.thumbnailSize.height);
                _photoViewFrame = CGRectMake(KSCREEN_WIDTH-margin-headImageWH-4-message.thumbnailSize.width, 4, _bubbleFrame.size.width, _bubbleFrame.size.height);
                _maxCellHeight = message.thumbnailSize.height + 2*marginTop;
                
            }
                break;
                
            default:
                break;
        }
        
        
    }
    
}

@end
